package swing.frame.whiteboard;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @author ywjia
 */
public class ColorPanel extends JPanel {
    public ColorPanel(){
        init();
    }
    JPanel panel =new JPanel();
    JLabel currentColorLabel;
    private Color bColor= Color.BLACK;
    private final Color fColor=Color.WHITE;
    public Color getbColor(){
        return bColor;
    }
    public Color getfColor(){
        return fColor;
    }
    public void init(){
        this.setLayout(new FlowLayout(FlowLayout.LEFT));

        BevelBorder bevelBorder = new BevelBorder(0, Color.gray,Color.white);
        JPanel left = new JPanel();
        left.setBackground(Color.white);
        left.setLayout(null);
        left.setBorder(bevelBorder);
        left.setPreferredSize(new Dimension(86,80));
        currentColorLabel = new JLabel();
        currentColorLabel.setBounds(18, 15, 50, 50);
        currentColorLabel.setOpaque(true);
        currentColorLabel.setBackground(Color.black);
        currentColorLabel.setBorder(new BevelBorder(1, Color.gray,Color.white));
        left.add(currentColorLabel);
        this.add(left);

        panel.setLayout(new GridLayout(2,14,0,0));

        Color [] color={new Color(0, 0, 0), new Color(128, 128, 128),
                new Color(128, 0, 0), new Color(128, 128, 0),
                new Color(0, 128, 0), new Color(0, 128, 128),
                new Color(0, 0, 128), new Color(128, 0, 128),
                new Color(128, 128, 64), new Color(0, 64, 64),
                new Color(0, 128, 255), new Color(0, 64, 128),
                new Color(128, 0, 255), new Color(128, 64, 0),

                new Color(255, 255, 255), new Color(192, 192, 192),
                new Color(255, 0, 0), new Color(255, 255, 0),
                new Color(0, 255, 0), new Color(0, 255, 255),
                new Color(0, 0, 255), new Color(255, 0, 255),
                new Color(255, 255, 128), new Color(0, 255, 128),
                new Color(128, 255, 255), new Color(128, 128, 255),
                new Color(255, 0, 128), new Color(255, 128, 64)};
        for(int i=0;i<color.length;i++){
            JPanel button=new JPanel();
            int finalI = i;
            //添加事件监听机制
            button.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    bColor=color[finalI];
                    currentColorLabel.setBackground(bColor);
                }
            });
            // 设置按钮的大小
            button.setPreferredSize(new Dimension(40,40));
            // 设置颜色选择按钮的颜色
            button.setBackground(color[i]);
            button.setBorder(bevelBorder);
            panel.add(button);
        }
//        panel.setPreferredSize(new Dimension(1300,100));
        this.add(panel);

    }
}
